#include "GraphicsDisplayPlugin.h"
#include <QPair>
#include <QDebug>
GraphicsDisplayPlugin::GraphicsDisplayPlugin(QObject *parent) :
    CameraPluginInterface(parent)
{
    connect(this,&GraphicsDisplayPlugin::closeGraphicsDisplayWidget,this,&GraphicsDisplayPlugin::closeImageDisplayWidget);
}

void GraphicsDisplayPlugin::initialize() {
    // 初始化插件，但不创建默认的显示区域，所有显示区域都由外部注册
    // 可以在这里进行一些基本的初始化工作
}
GraphicsDisplayPlugin::~GraphicsDisplayPlugin()
{
    // 清理所有没有注册的图形显示区域
    QMapIterator<QString, GraphicsViewArea*> it(m_UnregisterViewAreas);
    while (it.hasNext()) {
        it.next();
        delete it.value();
    }
    m_UnregisterViewAreas.clear();
    
    // 注意：注册的显示区域由MainWindow的QMdiArea管理其生命周期
    // 这里仅清空映射表，避免悬空指针
    m_RegisterViewAreas.clear();
}

QString GraphicsDisplayPlugin::pluginName() const {
    return GRAPHICS_DISPLAY_PLUGIN_NAME;
}

QString GraphicsDisplayPlugin::description() const {
    return "提供图形显示功能，支持图像渲染、坐标标注、抗锯齿优化以及多视图管理";
}

QVariant GraphicsDisplayPlugin::registerImageDisplayWidget(const QVariant& graphicsGuid, const QVariant& titleString)
{
    if (!graphicsGuid.isValid() || graphicsGuid.toString().isEmpty()) {
        return false;
    }
    QString guid = graphicsGuid.toString();
    if(m_RegisterViewAreas.contains(guid))
    {
        return false;
    }
    QString title = titleString.isValid() ? titleString.toString() : "Image Display";
    // 获取或创建图形显示区域
    GraphicsViewArea* viewArea = getOrCreateViewArea(guid);
    if (!viewArea)
    {
        return false;
    }
    
    // 确保视图区域可见且重置必要的状态
    viewArea->show();
    viewArea->raise();
    
    // 注册到主窗口
    emit registerGraphicsDisplayWidget(guid, title, viewArea);
    m_RegisterViewAreas.insert(guid,viewArea);
    m_UnregisterViewAreas.remove(guid);
    return true;
}

QVariant GraphicsDisplayPlugin::updateImage(const QVariant& graphicsGuid, const QVariant& image)
{
    if (!graphicsGuid.isValid() || graphicsGuid.toString().isEmpty()) {
        return false;
    }
    
    QString guid = graphicsGuid.toString();
    QPixmap pixmap;
    
    if (image.canConvert<QPixmap>()) {
        pixmap = image.value<QPixmap>();
    } else if (image.canConvert<QImage>()) {
        pixmap = QPixmap::fromImage(image.value<QImage>());
    } else {
        return false;
    }
    
    if (pixmap.isNull()) {
        return false;
    }
    
    // 查找对应的视图区域
    GraphicsViewArea* viewArea = m_RegisterViewAreas.value(guid, nullptr);
    if (!viewArea) {
        return false;
    }
    
    // 更新图像
    viewArea->setImageGuid(guid);
    viewArea->setImage(pixmap);
    viewArea->update();
    return true;
}

QVariant GraphicsDisplayPlugin::getCurrentScale(const QVariant& graphicsGuid)
{
    qreal scale = 1.0;
    
    if (!graphicsGuid.isValid() || graphicsGuid.toString().isEmpty()) {
        // 如果未指定标识符，返回当前活动视图的缩放比例
        if (!m_currentActiveGuid.isEmpty()) {
            GraphicsViewArea* viewArea = m_RegisterViewAreas.value(m_currentActiveGuid, nullptr);
            if (viewArea) {
                scale = viewArea->getCurrentScale();
            }
        }
    } else {
        QString guid = graphicsGuid.toString();
        // 查找对应的视图区域
        GraphicsViewArea* viewArea = m_RegisterViewAreas.value(guid, nullptr);
        if (viewArea) {
            scale = viewArea->getCurrentScale();
        }
    }
    
    return scale;
}

QVariant GraphicsDisplayPlugin::setScale(const QVariant& graphicsGuid, const QVariant& scale)
{
    if (!graphicsGuid.isValid() || graphicsGuid.toString().isEmpty()) {
        return false;
    }
    
    QString guid = graphicsGuid.toString();
    
    // 尝试将scale参数转换为数值
    bool ok = false;
    qreal scaleValue = scale.toReal(&ok);
    if (!ok || scaleValue <= 0) {
        return false;
    }
    
    // 查找对应的视图区域
    GraphicsViewArea* viewArea = m_RegisterViewAreas.value(guid, nullptr);
    if (!viewArea) {
        return false;
    }
    
    // 调用视图区域的设置缩放比例方法
    viewArea->setScale(scaleValue);

    return true;
}

QVariant GraphicsDisplayPlugin::getCurrentOffset(const QVariant &graphicsGuid)
{
    if (!graphicsGuid.isValid() || graphicsGuid.toString().isEmpty())
    {
        return QSize(0,0);
    }
    else
    {
        QString guid = graphicsGuid.toString();
        // 查找对应的视图区域
        GraphicsViewArea* viewArea = m_RegisterViewAreas.value(guid, nullptr);
        if (viewArea)
        {
            return viewArea->getImageOffset();
        }
    }
    return QSize(0,0);
}

void GraphicsDisplayPlugin::onPluginMessage(const QString& sender, const QString& message)
{
    qDebug()<<"GraphicsDisplayPlugin from "<<sender<< "Message:"<<message;
    
    // 调用解析函数获取指令和参数
    auto [command, params] = parsePluginMessage(message);
    qDebug()<<"Command"<<command<< "Params:"<<params;
    // 根据解析得到的指令执行相应操作
    if (command == CMD_OPERATION_UNDO_SUCCESS || command == CMD_OPERATION_REDO_SUCCESS) {
        if (params.contains("guid") && params.contains("operationType")) {
            QString imageGuid = params["guid"].toString();
            QString operationType = params["operationType"].toString();
            
            qDebug() << "Processing " << command << " for image: " << imageGuid << ", operationType: " << operationType;
            
            // 获取当前操作值
            QVariantList args;
            args << imageGuid;
            QVariant operationValue;
            bool success = requestPluginFunctionCall(
                IMAGE_MANAGEMENT_PLUGIN_NAME, 
                "getCurrentOperationValue", 
                args, 
                operationValue
            );
            
            if (success && operationValue.isValid() && operationValue.canConvert<QVariantMap>()) {
                QVariantMap operationParams = operationValue.value<QVariantMap>();
                qDebug() << "Successfully got operation parameters: " << operationParams;
                
                // 根据操作类型和指令类型处理不同的参数
                if (operationType == "AxisParamsChange") {
                    if (command == CMD_OPERATION_UNDO_SUCCESS && operationParams.contains("previousValue")) {
                        QVariantMap previousParams = operationParams["previousValue"].toMap();
                        qDebug() << "Setting previous axis parameters: " << previousParams;
                        
                        // 提取长度比例和单位信息
                        if (previousParams.contains("lengthRatio") && previousParams.contains("lengthUnit")) {
                            double lengthRatio = previousParams["lengthRatio"].toDouble();
                            QString lengthUnit = previousParams["lengthUnit"].toString();
                            
                            // 使用requestPluginFunctionCall更新图像转换信息
                            QVariantList updateArgs;
                            updateArgs << imageGuid << lengthRatio << 1.0 << 1.0 << lengthUnit << "s" << "nm";
                            QVariant returnValue;
                            requestPluginFunctionCall(
                                IMAGE_MANAGEMENT_PLUGIN_NAME, 
                                "setImageConversionInfo", 
                                updateArgs, 
                                returnValue
                            );
                            
                            qDebug() << "Updated axis parameters for undo: " << lengthRatio << lengthUnit;
                        }
                    }
                    else if (command == CMD_OPERATION_REDO_SUCCESS && operationParams.contains("newValue")) {
                        QVariantMap newParams = operationParams["newValue"].toMap();
                        qDebug() << "Setting new axis parameters: " << newParams;
                        
                        // 提取长度比例和单位信息
                        if (newParams.contains("lengthRatio") && newParams.contains("lengthUnit")) {
                            double lengthRatio = newParams["lengthRatio"].toDouble();
                            QString lengthUnit = newParams["lengthUnit"].toString();
                            
                            // 使用requestPluginFunctionCall更新图像转换信息
                            QVariantList updateArgs;
                            updateArgs << imageGuid << lengthRatio << 1.0 << 1.0 << lengthUnit << "s" << "nm";
                            QVariant returnValue;
                            requestPluginFunctionCall(
                                IMAGE_MANAGEMENT_PLUGIN_NAME, 
                                "setImageConversionInfo", 
                                updateArgs, 
                                returnValue
                            );
                            
                            qDebug() << "Updated axis parameters for redo: " << lengthRatio << lengthUnit;
                        }
                    }
                }
                else if (operationType == "ImageCalibration") {
                    // 处理图像标定操作
                    if (command == CMD_OPERATION_UNDO_SUCCESS && operationParams.contains("previousValue")) {
                        QVariantMap previousParams = operationParams["previousValue"].toMap();
                        qDebug() << "Setting previous calibration parameters: " << previousParams;
                        
                        // 实现标定参数的恢复逻辑
                        // 这里可以根据实际的标定参数结构进行处理
                    }
                    else if (command == CMD_OPERATION_REDO_SUCCESS && operationParams.contains("newValue")) {
                        QVariantMap newParams = operationParams["newValue"].toMap();
                        qDebug() << "Setting new calibration parameters: " << newParams;
                        
                        // 实现标定参数的恢复逻辑
                        // 这里可以根据实际的标定参数结构进行处理
                    }
                }
                else if (operationType == "ImageScale") {
                    // 处理图像缩放操作
                    if (command == CMD_OPERATION_UNDO_SUCCESS && operationParams.contains("previousValue")) {
                        double scaleValue = operationParams["previousValue"].toDouble();
                        qDebug() << "Setting previous scale value: " << scaleValue;
                        
                        // 设置缩放比例
                        setScale(imageGuid, scaleValue);
                        qDebug() << "Updated scale for undo: " << scaleValue;
                    }
                    else if (command == CMD_OPERATION_REDO_SUCCESS && operationParams.contains("newValue")) {
                        double scaleValue = operationParams["newValue"].toDouble();
                        qDebug() << "Setting new scale value: " << scaleValue;
                        
                        // 设置缩放比例
                        setScale(imageGuid, scaleValue);
                        qDebug() << "Updated scale for redo: " << scaleValue;
                    }
                }
            }
        }
    }
    else if (command == CMD_REGISTER_IMAGE_DISPLAY) {
        if (params.contains("guid") && params.contains("title")) {
            registerImageDisplayWidget(params["guid"], params["title"]);
        }
    }
    else if (command == CMD_UPDATE_IMAGE_DISPLAY) {
        if (params.contains("guid")) {
            QString guid = params["guid"].toString();
            
            // 主动调用ImageManagementPlugin的接口获取原始图像数据
            QVariantList args;
            args << guid;
            QVariant imageData;
           emit requestPluginFunctionCall(
                IMAGE_MANAGEMENT_PLUGIN_NAME, 
                "getOriginalImage", 
                args, 
                imageData
            );
            
            if (imageData.isValid() && (imageData.canConvert<QPixmap>() || imageData.canConvert<QImage>())) {
                updateImage(guid, imageData);
            }
        }
    }
    else if (command == CMD_UPDATE_PROCESSED_IMAGE) {
        if (params.contains("guid")) {
            QString guid = params["guid"].toString();
            
            // 主动调用ImageManagementPlugin的接口获取处理后图像数据
            QVariantList args;
            args << guid;
            QVariant imageData;
            emit requestPluginFunctionCall(
                IMAGE_MANAGEMENT_PLUGIN_NAME, 
                "getProcessedImage", 
                args, 
                imageData
            );
            
            if (imageData.isValid() && (imageData.canConvert<QPixmap>() || imageData.canConvert<QImage>())) {
                updateImage(guid, imageData);
            }
        }
    }
    else if (command == CMD_UNREGISTER_IMAGE_DISPLAY) {
        if (params.contains("guid")) {
            unregisterImageDisplayWidget(params["guid"]);
        }
    }
    else if (command == CMD_SET_SCALE) {
        if (params.contains("guid") && params.contains("scale")) {
            setScale(params["guid"], params["scale"]);
        }
    }
    else if (command == CMD_IMAGE_SELECTED) {
        if (params.contains("guid")) {
            QString imageGuid = params["guid"].toString();
            // 更新当前活动的图像GUID
            if (m_RegisterViewAreas.contains(imageGuid)) {
                m_currentActiveGuid = imageGuid;
            }
        }
    }
}

QPair<QString, QVariantMap> GraphicsDisplayPlugin::parsePluginMessage(const QString &message)
{
    QString command; // 指令名称
    QVariantMap params; // 参数映射
    
    // 处理undo和redo消息
    if (message.startsWith(CMD_OPERATION_UNDO_SUCCESS":")) {
        // 解析消息，格式为：CMD_OPERATION_UNDO_SUCCESS:图像GUID:操作类型:描述
        QStringList parts = message.split(":");
        if (parts.size() >= 4) {
            command = CMD_OPERATION_UNDO_SUCCESS;
            params["guid"] = parts[1];
            params["operationType"] = parts[2];
            params["description"] = parts[3];
        }
    }
    // 处理redo成功消息
    else if (message.startsWith(CMD_OPERATION_REDO_SUCCESS":")) {
        // 解析消息，格式为：CMD_OPERATION_REDO_SUCCESS:图像GUID:操作类型:描述
        QStringList parts = message.split(":");
        if (parts.size() >= 4) {
            command = CMD_OPERATION_REDO_SUCCESS;
            params["guid"] = parts[1];
            params["operationType"] = parts[2];
            params["description"] = parts[3];
        }
    }
    else if (message.startsWith(CMD_IMAGE_SELECTED ":")) {
        // 解析消息，格式为：IMAGE_SELECTED:图像GUID
        QStringList parts = message.split(":");
        if (parts.size() >= 2) {
            command = CMD_IMAGE_SELECTED;
            params["guid"] = parts[1];
        }
    }
    // 解析其他标准格式指令
    else if (message.startsWith(CMD_REGISTER_IMAGE_DISPLAY ":")) {
        // 解析消息，格式为：REGISTER_IMAGE_DISPLAY:图像GUID:标题
        QStringList parts = message.split(":");
        if (parts.size() >= 3) {
            command = CMD_REGISTER_IMAGE_DISPLAY;
            params["guid"] = parts[1];
            params["title"] = (parts.size() > 3 ? "Image Display: " + parts[2] : parts[2]);
        }
    }
    else if (message.startsWith(CMD_UPDATE_IMAGE_DISPLAY ":")) {
        // 解析消息，格式为：UPDATE_IMAGE_DISPLAY:图像GUID
        QStringList parts = message.split(":");
        if (parts.size() >= 2) {
            command = CMD_UPDATE_IMAGE_DISPLAY;
            params["guid"] = parts[1];
        }
    }
    else if (message.startsWith(CMD_UPDATE_PROCESSED_IMAGE ":")) {
        // 解析消息，格式为：UPDATE_PROCESSED_IMAGE:图像GUID
        QStringList parts = message.split(":");
        if (parts.size() >= 2) {
            command = CMD_UPDATE_PROCESSED_IMAGE;
            params["guid"] = parts[1];
        }
    }
    else if (message.startsWith(CMD_UNREGISTER_IMAGE_DISPLAY ":")) {
        // 解析消息，格式为：UNREGISTER_IMAGE_DISPLAY:图像GUID
        QStringList parts = message.split(":");
        if (parts.size() >= 2) {
            command = CMD_UNREGISTER_IMAGE_DISPLAY;
            params["guid"] = parts[1];
        }
    }
    else if (message.startsWith(CMD_SET_SCALE ":")) {
        // 解析消息，格式为：SET_SCALE:图像GUID:缩放比例
        QStringList parts = message.split(":");
        if (parts.size() >= 3) {
            command = CMD_SET_SCALE;
            params["guid"] = parts[1];
            params["scale"] = parts[2].toDouble();
        }
    }
    // 兼容性支持 - 将旧格式指令转换为标准格式
    else if (message.startsWith("registerDisplay:")) {
        // 旧格式：registerDisplay:{guid}:{title}
        QStringList parts = message.split(":");
        if (parts.size() >= 3) {
            command = CMD_REGISTER_IMAGE_DISPLAY;
            params["guid"] = parts[1];
            params["title"] = parts[2];
        }
    }
    else if (message.startsWith("updateImage:")) {
        // 旧格式：updateImage:{guid}:{imageData}
        QStringList parts = message.split(":");
        if (parts.size() >= 3) {
            command = CMD_UPDATE_IMAGE_DISPLAY;
            params["guid"] = parts[1];
            if (parts.size() > 3) {
                // 实际应用中需要解析图像数据
                params["imageData"] = QPixmap();
            }
        }
    }
    else if (message.startsWith("setScale:")) {
        // 旧格式：setScale:{guid}:{scale}
        QStringList parts = message.split(":");
        if (parts.size() >= 3) {
            command = CMD_SET_SCALE;
            params["guid"] = parts[1];
            params["scale"] = parts[2].toDouble();
        }
    }
    
    return {command, params};
}

void GraphicsDisplayPlugin::onConversionInfoChanged(const QString &imageGuid, double lengthRatio, double timeRatio, double wavelengthRatio,
                                                    const QString &lengthUnit, const QString &timeUnit, const QString &wavelengthUnit,
                                                    int xUnit ,int yUnit)
{
    QVariantList args;
    args << imageGuid << lengthRatio << timeRatio << wavelengthRatio << lengthUnit << timeUnit << wavelengthUnit<<xUnit<<yUnit;
    QVariant returnValue;
    emit requestPluginFunctionCall(
        IMAGE_MANAGEMENT_PLUGIN_NAME, 
        "setImageConversionInfo", 
        args, 
        returnValue
    );
    QString message = QString(CMD_UNIT_CONVERSION_CHANGED ":%1:%2:%3:%4:%5:%6:%7:%8:%9").arg(imageGuid).arg(lengthRatio).arg(timeRatio).arg(wavelengthRatio)
            .arg(lengthUnit).arg(timeUnit).arg(wavelengthUnit).arg(xUnit).arg(yUnit);
    emit pluginMessageSent(GRAPHICS_DISPLAY_PLUGIN_NAME, message, MEASUREMENT_MANAGEMENT_PLUGIN_NAME);
}

QVariant GraphicsDisplayPlugin::unregisterImageDisplayWidget(const QVariant& graphicsGuid)
{
    if (!graphicsGuid.isValid() || graphicsGuid.toString().isEmpty()) {
        return false;
    }
    
    QString guid = graphicsGuid.toString();
    
    // 检查指定的GUID是否存在
    if (m_RegisterViewAreas.contains(guid))
    {
        // 获取要删除的图形显示区域
            GraphicsViewArea* viewArea = m_RegisterViewAreas.take(guid);
            if (viewArea) {
                // 如果删除的是当前活动的视图，更新当前活动视图
                if (m_currentActiveGuid == guid) {
                    if (!m_RegisterViewAreas.isEmpty()) {
                        m_currentActiveGuid = m_RegisterViewAreas.firstKey();
                    } else {
                        m_currentActiveGuid.clear();
                    }
                }
            }

            // 先发出信号通知主窗口关闭对应的显示窗口
            emit unregisterGraphicsDisplayWidget(guid);
            
            // 然后再删除视图区域对象，确保主窗口有机会根据GUID查找并处理窗口关闭逻辑
            if (viewArea) {
                delete viewArea;
            }
        // 注册视图区域注销成功
        return true;
    }
    else if(m_UnregisterViewAreas.contains(guid))
    {
        // 删除对应的图形显示区域
        GraphicsViewArea* viewArea = m_UnregisterViewAreas.take(guid);
        if (viewArea)
        {
            delete viewArea;
        }
        // 未注册视图区域清理成功
        return true;
    }
    // GUID不存在
    return false;
}

void GraphicsDisplayPlugin::closeImageDisplayWidget(const QString& graphicsGuid)
{
    // 检查指定的GUID是否存在
    if (!m_RegisterViewAreas.contains(graphicsGuid))
    {
        return;
    }
    // 关闭对应的图形显示区域
    GraphicsViewArea* viewArea = m_RegisterViewAreas.value(graphicsGuid);
    if (viewArea)
    {
        viewArea->hide();
        viewArea->setParent(nullptr);
        // 如果删除的是当前活动的视图，更新当前活动视图
        if (m_currentActiveGuid == graphicsGuid)
        {
            if (!m_RegisterViewAreas.isEmpty())
            {
                m_currentActiveGuid = m_RegisterViewAreas.firstKey();
            }
            else
            {
                m_currentActiveGuid.clear();
            }
        }
        m_RegisterViewAreas.remove(graphicsGuid);
        m_UnregisterViewAreas.insert(graphicsGuid,viewArea);
    }
}



GraphicsViewArea* GraphicsDisplayPlugin::getOrCreateViewArea(const QString& graphicsGuid)
{
    if (graphicsGuid.isEmpty()) {
        return nullptr;
    }
    
    // 检查是否已经存在
    if (m_UnregisterViewAreas.contains(graphicsGuid)) {
        return m_UnregisterViewAreas.value(graphicsGuid);
    }
    
    // 创建新的图形显示区域
    GraphicsViewArea* viewArea = new GraphicsViewArea();
    
    // 直接连接conversionInfoChanged信号到槽函数，不需要匿名函数
    connect(viewArea, &GraphicsViewArea::conversionInfoChanged, this, &GraphicsDisplayPlugin::onConversionInfoChanged);
    
    // 连接缩放变化信号到MeasurementManagementPlugin
    connect(viewArea, &GraphicsViewArea::scaleChanged, this, [=](const QString &imageGuid, qreal scale) {
        // 向MeasurementManagementPlugin发送缩放变化消息
        QString message = QString(CMD_SCALE_CHANGED ":%1:%2").arg(imageGuid).arg(scale);
        emit pluginMessageSent(GRAPHICS_DISPLAY_PLUGIN_NAME, message, MEASUREMENT_MANAGEMENT_PLUGIN_NAME);
    });
    
    // 连接撤销和重做信号到ImageManagementPlugin
    connect(viewArea, &GraphicsViewArea::undoRequested, this, [=](const QString &imageGuid) {
        // 请求ImageManagementPlugin执行撤销操作
        QVariantList args;
        args << imageGuid;
        QVariant returnValue;
        requestPluginFunctionCall(
            "Image Management Plugin",
            "undo",
            args,
            returnValue
        );
    });
    
    connect(viewArea, &GraphicsViewArea::redoRequested, this, [=](const QString &imageGuid) {
        // 请求ImageManagementPlugin执行重做操作
        QVariantList args;
        args << imageGuid;
        QVariant returnValue;
        requestPluginFunctionCall(
            "Image Management Plugin",
            "redo",
            args,
            returnValue
        );
    });
    
    m_UnregisterViewAreas.insert(graphicsGuid, viewArea);
    return viewArea;
}

/**
 * @brief 获取更新显示图像回调函数
 * @param graphicsGuid 图形guid
 * @return 更新显示图像回调函数
 */
QVariant GraphicsDisplayPlugin::getUpdateDisplayImageCallback(const QVariant& graphicsGuid)
{
    QString guid=graphicsGuid.toString();
    return reinterpret_cast<qintptr>(updateDisplayImage);
}

/**
 * @brief 获取更新显示图像界面指针
 * @param graphicsGuid 图形guid
 * @return 更新显示图像界面指针
 */
QVariant GraphicsDisplayPlugin::getUpdateDisplayImageWidget(const QVariant& graphicsGuid)
{
    QString guid=graphicsGuid.toString();
    if(m_RegisterViewAreas.contains(guid))
    {
        return QVariant::fromValue(m_RegisterViewAreas.value(guid));
    }
    else if(m_UnregisterViewAreas.contains(guid))
    {
        return QVariant::fromValue(m_UnregisterViewAreas.value(guid));
    }
    return QVariant();
}

QVariant GraphicsDisplayPlugin::getScenePointer(const QVariant &graphicsGuid)
{
    QString guid = graphicsGuid.toString();
    GraphicsViewArea* viewArea = nullptr;
    
    // 首先在已注册视图中查找
    if(m_RegisterViewAreas.contains(guid))
    {
        viewArea = m_RegisterViewAreas.value(guid);
    }
    // 如果未找到，在未注册视图中查找
    else if(m_UnregisterViewAreas.contains(guid))
    {
        viewArea = m_UnregisterViewAreas.value(guid);
    }
    
    // 如果找到视图区域，返回其场景指针
    if(viewArea)
    {
        QGraphicsScene* scene = viewArea->getScene();
        if(scene)
        {
            return QVariant::fromValue(scene);
        }
    }
    
    // 如果未找到或场景为空，返回无效的QVariant
    return QVariant();
}
/**
 * @brief 更新显示图像
 * @param pUpdateWidget 更新的widget
 * @param image 待更新的图像
 */
void updateDisplayImage(const QWidget *pUpdateWidget, const QImage &image)
{
    GraphicsViewArea* pWidget=(GraphicsViewArea*)pUpdateWidget;
    pWidget->updateImage(QPixmap::fromImage(image));
    pWidget->update();
}
